/* 
 * Copyright (C) 2006, Dung-Bang Tsai
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * ( If you wnat to use this library for commercial use, 
 *	  feel free to contact me, just cost some money, I could sell
 *		you the code without GPL license, so you could use this code
 *		for your product without public your source code.  				)
 *
 * Authors:
 *	Tsai, Dung-Bang <dbtsai@gmail.com>
 *			
*			2006/03/08		at NCKU physics
 */

#include "TDBConvertEngine.h"
#include "tables/StoTtable.h"
#include "tables/TtoStable.h"

void TDBstring :: Traditionalized()
{
	StoT_mapping();
}

void TDBstring :: Simplized()
{
 	TtoS_mapping();
}

void TDBstring :: StoT_mapping()
{
	std::vector<unsigned int>::iterator beg = data.begin();
	const std::vector<unsigned int>::iterator end = data.end();
	
	register unsigned int temp(0);
	for( ;beg!=end ; beg++)
	{
		temp	= StoT_map( *beg ) |  (0x01 << 29) ;
		if(temp != *beg){*beg = temp;}
	}
/*enum // You may define by using or | operator
	map=0x01,  						 // 是否查表直接對應
	correction = 0x02,     //字彙修正
	dest_exit = 0x04        // 目標編碼是否有這個字
*/
}

void TDBstring  :: TtoS_mapping()
{
	std::vector<unsigned int>::iterator beg = data.begin();
	const std::vector<unsigned int>::iterator end = data.end();
	
	register unsigned int temp(0);
	for( ;beg!=end ; beg++)
	{
		temp	= TtoS_map( *beg ) |  (0x01 << 29) ;
		if(temp != *beg){	*beg = temp;}
	}
/*enum // You may define by using or | operator
	map=0x01,  						 // 是否查表直接對應
	correction = 0x02,     //字彙修正
	dest_exit = 0x04        // 目標編碼是否有這個字
*/
}

void TDBstring ::push_back( unsigned int& ucs4)
{
	if(ucs4 == 0x0A ) // Unix
	{
		if(if_0D_flag!=true)  // 則剛剛沒有換行過，換行之
		{
			NewLineType= Unix;
			// 0x01 << 29 mean unix type, due to ucs4 is up to 0x0010FFFF
			// So, we use the unuse bit to record extra information
			data.push_back(0x0A | (0x01 << 29) ); 
			++n_line; 
			return;
		}
			// 剛剛有換行過，但是不知道是DOS type or Mac type, 
			//但是現在確定了，所以修正NewLineType為DOS type.
		NewLineType= DOS; 
		*(--data.end()) = (0x0A | (0x03 << 29) ); 
		if_0D_flag = false;
		return;
	}
    if(ucs4 == 0x0D) // Mac or DOS, depend on next words.
	{ 
		 NewLineType= Mac;
		data.push_back (0x0A | (0x02 << 29) );
		++n_line;
		if_0D_flag = true;
		return;
	}
	
	if_0D_flag = false;
	// 0x01 << 30 mean unix type, due to ucs4 is up to 0x0010FFFF
	// So, we use the unuse bit to record extra information
	data.push_back(ucs4);
	return;
}
